﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text.RegularExpressions;
namespace Tal.CaptchaControl
{
    /// <summary>
    /// Summary description for SixCharsTextGenerator
    /// </summary>
    public class SixCharsTextGenerator:TextGenerator
    {
        public SixCharsTextGenerator()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        /// <summary>
        /// Generates a 6 characters long random text
        /// </summary>
        /// <param name="charactersTypes">The type of the characters in the text</param>
        /// <returns>A 6 characters long string</returns>
        /// <exception cref="ArgumentOutOfRangeException"></exception>
        public override string Generate(TextGenerator.CharacterType charactersTypes)
        {
            if (charactersTypes == CharacterType.AlphaNumeric)
            {

                return Guid.NewGuid().ToString("N").Substring(0, 6);
            }
            else if (charactersTypes == CharacterType.LettersOnly)
            {
                string imageText;
                do
                {
                    imageText = Regex.Replace(Guid.NewGuid().ToString("N"), @"[^a-zA-Z]", string.Empty);
                    if (imageText.Length > 6)
                    {
                        imageText = imageText.Substring(0, 6);
                    }
                } while (imageText.Length < 6);
                return imageText;
            }
            else if (charactersTypes == CharacterType.NumbersOnly)
            {
                return new Random().Next(100000, 999999).ToString();
            }
            else
            {
                throw new ArgumentOutOfRangeException("charactersTypes");
            }
        }
    }
}
